<?php

namespace Biz\model\springHomeDecActivity;

use Biz\model\advertisement\MBaseDbModel;
use Biz\pockets\modelTrait\BizModelTrait;

class MSpringHomeDecUserLottery extends MBaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';
    
    const _CONFIG_SELECT_ = 'db_brand_activity';
    
    const _DATABASE_ = 'brand_activity';
    
    const _TABLE_NAME = 'spring_home_dec_user_lottery';


    //删除字段名称
    protected static $deleteField = 'status';
    
    
    use BizModelTrait;


    public static function getUserLotteryHitTimes($uids)
    {
        $list = self::dataSelect(['uid' => $uids, 'status' => self::STATUS_NORMAL]);
        $map = empty($list) ? [] : array_column($list, 'num', 'uid');
        return is_array($uids) ? $map : ($map[$uids] ?? false);
    }

    public static function getV($uid)
    {
        return self::dataSelectInfo(['uid' => $uid], ['v']);
    }

    public static function addVoteRecord($uid, $v, $is_new)
    {
        $date = date("Y-m-d H:i:s");
        if ($v) {
            $where = ['uid' => $uid, 'v' => $v];
            if ($is_new) $where['vote'] = 0;
            return self::dataUpdate(['vote[+]' => 1, 'update_time' => $date, 'v[+]' => 1], $where, true);
        }

        $data = [
            'uid' => $uid,
            'v' => 1,
            'vote' => 1,
            'create_time' => $date,
            'status' => self::STATUS_NORMAL,
        ];
        return self::dataInsert($data);
    }

    public static function addRecord($uid, $v, $max)
    {
        $date = date("Y-m-d H:i:s");
        if ($v) {
            return self::dataUpdate(['num[+]' => 1, 'update_time' => $date, 'v[+]' => 1], ['uid' => $uid, 'v' => $v, 'num[<=]' => $max], true);
        }

        $data = [
            'uid' => $uid,
            'v' => 1,
            'num' => 1,
            'create_time' => $date,
            'status' => self::STATUS_NORMAL,
        ];
        return self::dataInsert($data);
    }
}
